package com.spice.common.exception;

import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import lombok.Data;

/**
 * @author yhm
 * @date 2020/7/19 10:09
 * Description: 自定义异常类
 * 可以通过这个类构造出一个运行时异常然后抛出
 * 运行时异常对代码没有侵入性,只要在写的代码的catch方法中new并抛出即可，不会强制调用者去try-catch
 */
@Data
public class SpiceException extends RuntimeException {

    private static final long serialVersionUID = -6550993268323357055L;
    /**
     * 状态码
     */
    private String code;

    private String message;

    public SpiceException() {
        this.code = "400";
        this.message = "";
    }

    /**
     * 接收错误码和异常类
     *
     * @param errorCode
     * @param cause
     */
    public SpiceException(String errorCode, Throwable cause) {
        this(errorCode, "", cause);
    }

    /**
     * 接收错误码、异常类和错误信息
     *
     * @param errorCode
     * @param cause
     * @param message
     */
    public SpiceException(String errorCode, String message, Throwable cause) {
        super(cause);
        this.code = errorCode;
        this.message = message;
    }

    /**
     * 接收枚举类型
     *
     * @param resultCodeEnum
     */
    public SpiceException(ResultCodeEnum resultCodeEnum) {
        this(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), new RuntimeException());
    }

    public SpiceException message(String message){
        this.setMessage(message);
        return this;
    }

    public SpiceException code(String code){
        this.setCode(code);
        return this;
    }
}